package com.nce.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

/**
 * 登录用户工具
 * @author gt
 *
 */
public class LoginUserUtil extends SpringSecurityUtils {
	/**
	 * 返回登录的用户
	 * @return
	 */
	public static com.nce.entity.account.User  getUser() {
		Authentication authentication = getAuthentication();

		if (authentication == null) {
			return null;
		}

		Object principal = authentication.getPrincipal();
		if (!(principal instanceof WrapperUser)) {
			return null;
		}
		WrapperUser wu = (WrapperUser)principal;
		return wu.getUser();
	}
	
	
	
	/**
	 * 取得Authentication, 如当前SecurityContext为空时返回null.
	 */
	private static Authentication getAuthentication() {
		SecurityContext context = SecurityContextHolder.getContext();

		if (context == null) {
			return null;
		}

		return context.getAuthentication();
	}
}
